<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="false">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-collapse v-model="activeKey">
          <a-collapse-panel key="1" header="基本信息">
            <a-row :gutter="48">
              <a-col :span="8">
                <a-form-model-item label="离职人员" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="employeeId">
                  <select-person-input :disabled="formDisabled" v-model="model.employeeId" disabled/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="人员编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="employeeNo">
                  <a-input :disabled="formDisabled" v-model="model.employeeNo" placeholder="请输入人员编号" disabled></a-input>
                </a-form-model-item>
              </a-col>
              <!-- <a-col :span="8">
                <a-form-model-item label="所在单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="workUnit">
                  <j-select-depart :disabled="formDisabled" v-model="model.workUnit" multi disabled/>
                </a-form-model-item>
              </a-col> -->
              <a-col :span="8">
                <a-form-model-item label="所在部门" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="departmentId">
                  <j-select-depart :disabled="formDisabled" v-model="model.departmentId" multi disabled/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="所属团队" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="teamId">
                  <select-team-input :disabled="formDisabled" v-model="model.teamId" :tdlx="3" disabled/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="所属公司" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="workUnit">
                  <!-- <j-select-depart :disabled="formDisabled" v-model="model.workUnit"/> -->
                  <select-Company-input :disabled="formDisabled" placeholder="请选择所属公司" v-model="model.workUnit"/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="岗位名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="positionId">
                  <j-dict-select-tag
                    type="list"
                    v-model="model.positionId"
                    dictCode="sys_position,name,id"
                    placeholder="请选择岗位名称"
                    disabled/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="入职日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="entryTime">
                  <j-date
                    :disabled="formDisabled"
                    placeholder="请选择入职日期"
                    v-model="model.entryTime"
                    style="width: 100%"
                    disabled
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item
                  label="合同到期日期"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol"
                  prop="contractTime"
                >
                  <j-date
                    :disabled="formDisabled"
                    placeholder="请选择合同到期日期"
                    v-model="model.contractTime"
                    style="width: 100%"
                    disabled
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="员工类别" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="employeeType">
                  <j-dict-select-tag
                    :disabled="formDisabled"
                    type="list"
                    v-model="model.employeeType"
                    dictCode="employee_type"
                    placeholder="请选择员工类别"
                    disabled
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="离职类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="resignType">
                  <j-dict-select-tag
                    :disabled="formDisabled"
                    type="list"
                    v-model="model.resignType"
                    dictCode="resign_type"
                    placeholder="请选择离职类型"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="离职原因" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="resignReason">
                  <a-textarea
                    :disabled="formDisabled"
                    v-model="model.resignReason"
                    rows="4"
                    placeholder="请输入离职原因"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
                  <a-textarea :disabled="formDisabled" v-model="model.remark" rows="4" placeholder="请输入备注" />
                </a-form-model-item>
              </a-col>
            </a-row>
          </a-collapse-panel>
          <a-collapse-panel key="2" header="评价信息">
            <a-row :gutter="48">
              <a-col :span="8">
                <a-form-model-item label="离职日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="resignTime">
                  <j-date
                    :disabled="formDisabled"
                    placeholder="请选择离职日期"
                    v-model="model.resignTime"
                    style="width: 100%"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item
                  label="薪资截止日期"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol"
                  prop="salaryEndDate"
                >
                  <j-date
                    :disabled="formDisabled"
                    placeholder="请选择薪资截止日期"
                    v-model="model.salaryEndDate"
                    style="width: 100%"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item
                  label="社保截止日期"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol"
                  prop="socialSecurityDeadline"
                >
                  <j-date
                    :disabled="formDisabled"
                    placeholder="请选择社保截止日期"
                    v-model="model.socialSecurityDeadline"
                    style="width: 100%"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item
                  label="公积金截止日期"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol"
                  prop="accumulationFundDate"
                >
                  <j-date
                    :disabled="formDisabled"
                    placeholder="请选择公积金截止日期"
                    v-model="model.accumulationFundDate"
                    style="width: 100%"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="资产/物品" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="assets">
                  <a-input :disabled="formDisabled" v-model="model.assets" placeholder="请输入资产/物品"></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="借款/次数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="loanMoney">
                  <a-input :disabled="formDisabled" v-model="model.loanMoney" placeholder="请输入借款/次数"></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="工作交接" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="workHandover">
                  <select-person-input :disabled="formDisabled" v-model="model.workHandover"/>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="档案资料" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="profiles">
                  <a-input :disabled="formDisabled" v-model="model.profiles" placeholder="请输入档案资料"></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item
                  label="培训扣款（元）"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol"
                  prop="trainingDeduction"
                >
                  <a-input-number
                    :disabled="formDisabled"
                    :max="99999999"
                    :min="0"
                    v-model="model.trainingDeduction"
                    placeholder="请输入培训扣款"
                    style="width: 100%"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item
                  label="电脑扣款（元）"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol"
                  prop="computerDeduction"
                >
                  <a-input-number
                    :disabled="formDisabled"
                    :max="99999999"
                    :min="0"
                    v-model="model.computerDeduction"
                    placeholder="请输入电脑扣款"
                    style="width: 100%"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="其他扣款（元）" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="otherDeduction">
                  <a-input-number
                    :disabled="formDisabled"
                    :max="99999999"
                    :min="0"
                    v-model="model.otherDeduction"
                    placeholder="请输入其他扣款"
                    style="width: 100%"
                  />
                </a-form-model-item>
              </a-col>
              <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
                <a-button @click="submitForm">提 交</a-button>
              </a-col>
            </a-row>
          </a-collapse-panel>
          <a-collapse-panel key="3" header="附件信息">
            <a-row>
              <a-col :span="24">
                <a-form-model-item label="附件" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="attachment">
                  <j-upload :disabled="formDisabled" v-model="model.attachment"></j-upload>
                </a-form-model-item>
              </a-col>
            </a-row>
          </a-collapse-panel>
        </a-collapse>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, postAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import SelectPersonInput from '@/views/scy/components/SelectPersonInput'
import SelectTeamInput from '@/views/scy/components/SelectTeamInput'
import { mapGetters } from 'vuex';

export default {
  name: 'ErpHrEmployeeResignBillForm',
  components: {
    SelectPersonInput,
    SelectTeamInput
  },
  props: {
    // 流程表单data
    formData: {
      type: Object,
      default: () => {},
      required: false
    },
    // 表单模式：true流程表单 false普通表单
    formBpm: {
      type: Boolean,
      default: false,
      required: false
    },
    // 表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      activeKey: ['1', '2', '3'],
      model: {},
      labelCol: {},
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 24 }
      },
      confirmLoading: false,
      validatorRules: {
        workUnit: [
          { required: true, message: '请选择所属公司!' }
        ],
        resignType: [
            { required: true, message: '请选择离职类型!' }
        ],
        resignReason: [
            { required: true, message: '请输入离职原因!' }
        ],
        resignTime: [
            { required: true, message: '请选择离职日期!' }
        ],
        salaryEndDate: [
            { required: true, message: '请选择薪资截止日期!' }
        ],
        socialSecurityDeadline: [
            { required: true, message: '请选择社保截止日期!' }
        ],
        accumulationFundDate: [
            { required: true, message: '请选择社公积金截止日期!' }
        ],
        assets: [
            { required: true, message: '请输入资产/物品!' }
        ],
        loanMoney: [
            { required: true, message: '请输入借款/次数!' }
        ],
        workHandover: [
            { required: true, message: '请选择工作交接人!' }
        ],
        profiles: [
            { required: true, message: '请输入档案资料!' }
        ],
        trainingDeduction: [
            { required: true, pattern: /^(([1-9][0-9]*|0)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入正确的培训扣款!' }
        ],
        computerDeduction: [
            { required: true, pattern: /^(([1-9][0-9]*|0)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入正确的电脑扣款!' }
        ],
        otherDeduction: [
            { required: false, pattern: /^(([1-9][0-9]*|0)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入正确的其他扣款!' }
        ]
      },
      url: {
        add: '/api/erp/hr/resign/erpHrEmployeeResignBill/add',
        edit: '/api/erp/hr/resign/erpHrEmployeeResignBill/edit',
        queryById: '/api/erp/hr/resign/erpHrEmployeeResignBill/queryById',
        queryContractByEmployeeId: '/api/erp/hr/contract/erpHrEmployeeContract/queryByEmployeeId'
      }
    }
  },
  computed: {
    formDisabled() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return false
        }
        return true
      }
      return this.disabled
    },
    showFlowSubmitButton() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return true
        }
      }
      return false
    }
  },
  created() {
    // 备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
    // 如果是流程中表单，则需要加载流程表单data
    this.showFlowData()
  },
  methods: {
    ...mapGetters(['userInfo']),
    add() {
      let userinfo = this.userInfo();
      this.$set(this.modelDefault, 'employeeId', userinfo.empId);
      this.$set(this.modelDefault, 'employeeNo', userinfo.employee.employeeNo);
      // this.$set(this.modelDefault, 'workUnit', userinfo.employee.orgDepart);
      this.$set(this.modelDefault, 'departmentId', userinfo.employee.departmentId);
      this.$set(this.modelDefault, 'teamId', userinfo.employee.teamId);
      this.$set(this.modelDefault, 'positionId', userinfo.employee.mainPost);
      this.$set(this.modelDefault, 'entryTime', userinfo.employee.joinDate);
      this.$set(this.modelDefault, 'employeeType', userinfo.employee.employeeType);
      getAction(this.url.queryContractByEmployeeId, { employeeId: userinfo.empId }).then(res => {
        console.log(res);
        if (res.success) {
          let data = res.result;
          if (data) {
            this.$set(this.modelDefault, 'contractTime', data[0].endDate);
          }
          this.edit(this.modelDefault)
        }
      })
    },
    edit(record) {
      this.$refs.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
    },
    // 渲染流程表单数据
    showFlowData() {
      if (this.formBpm === true) {
        let params = { id: this.formData.dataId }
        getAction(this.url.queryById, params).then((res) => {
          if (res.success) {
            this.edit(res.result)
          }
        })
      }
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    }
  }
}
</script>
